#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int f[N];
int arr[N];
int g[N];
int n;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}
	for (int i = 1; i <= n; i++) {
		f[i] = 1;
		for (int j = 1; j < i; j++) {
			if (arr[i] > arr[j]) {
				if (f[i] < f[j] + 1) {
					f[i] = f[j] + 1;
					g[i] = j;
				}
			}
		}
	}
	int k = 1;
	for (int i = 0; i <= n; i++) {	
		if (f[k] < f[i]) {
			k = i;
		}
	}
	int stk[1010] = { 0 };
	int hh = 0, tt = -1;
	cout << f[k] << endl;
	int len = f[k];
	for (int i = 0; i < len; i++) {
		stk[++tt] = arr[k];
		k = g[k];	
	}
	for (int i = 0; i < len; i++) {
		cout << stk[tt--] << " ";
	}
	return 0;
}